<?php
defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

class TravelModelFields extends JModelList
{
/**
	 * Constructor.
	 *
	 * @param	array	An optional associative array of configuration settings.
	 * @see		JController
	 * @since	1.6
	 */
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'id', 'f.id',
				'field_name', 'f.field_name',
				'field_title', 'f.field_title',
				'field_type', 'f.field_type',
				'required', 'f.required',
				'published', 'f.published',
				'ordering', 'f.ordering',
				'position_display', 'f.position_display',
				'catsid', 'f.catsid',
			);
		}
		parent::__construct($config);
	}

	protected function populateState($ordering = null, $direction = null)
	{
		// Initialise variables.
		$app = JFactory::getApplication('administrator');
	
		// Load the filter search.
		$search = $app->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);
		
		// Load the filter published.
		$published = $this->getUserStateFromRequest($this->context.'.filter.published', 'filter_published', '');
		$this->setState('filter.published', $published);
		
		// Load the filter field type.
		$fieldType = $app->getUserStateFromRequest($this->context.'.filter.field_type', 'filter_field_type');
		$this->setState('filter.field_type', $fieldType);
	
		// Load the parameters.
		$params = JComponentHelper::getParams('com_travel');
		$this->setState('params', $params);
	
		// List state information.
		parent::populateState('f.field_name', 'asc');
	}
	
	function getFields($onlyPublished=true,$filter_order="ordering",$filter_order_Dir="ASC") {
		if ($onlyPublished == true)
			$published = " published = 1 ";
		else
			$published = " 1 ";

		$this->_db->setQuery( "SELECT * FROM #__travel_fields WHERE $published ORDER by $filter_order $filter_order_Dir" );
		//f.published = 1
		$fields = $this->_db->loadObjectList('field_name');
		return $fields;
	}
	
	public function getListQuery() {
		// Initialise variables.
		$db		= $this->getDbo();
		$query	= $db->getQuery(true);
		$user	= JFactory::getUser();
		
		$query->select('f.id, f.field_name, f.field_title, f.field_type, f.required, f.published, f.ordering, f.position_display, f.checked_out, f.checked_out_time');
		$query->from('#__travel_fields as f');
		
		// Join over the users for the checked out user.
		$query->select('uc.name AS editor');
		$query->join('LEFT', '#__users AS uc ON uc.id=f.checked_out');
		
		//Filter by type
		$fieldType = $this->getState('filter.field_type');
		if(!empty($fieldType)){
			$fieldType = $db->escape($fieldType,true);
			$query->where("f.field_type = '$fieldType' ");
		}
		
		// Filter by published state
		$published = $this->getState('filter.published');
		if (is_numeric($published)) {
			$query->where('f.published = '.(int) $published);
		}
		
		//Filter by search title
		$search = $this->getState('filter.search');
		if(!empty($search)){
			$search = $db->escape($search,true);
			$query->where("(f.field_title LIKE '%".$search."%' OR f.field_name LIKE '%".$search."%' )");
		}
		
		//Add list ordering clause.
		$orderCol 	= $this->state->get('list.ordering','f.field_name');
		$orderDirn 	= $this->state->get('list.direction','asc');
		if ($orderCol == 'ordering' || $orderCol == 'position_display') {
			$orderCol = 'f.position_display '.$orderDirn.', f.ordering';
		}
		$query->order($db->escape($orderCol.' '.$orderDirn));
		return $query;
	}
}